"""
URL configuration for django_router_03 project.

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/5.1/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from django.contrib import admin
from django.shortcuts import render
from django.urls import path, include
from django.template.loader import get_template

from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login
from django.contrib import messages
from django.contrib.auth.models import User
from django.contrib.auth import views as auth_views



# 注册视图
def register_view(request):
    # return render(request, 'register.html')  # 渲染 templates/register.html

    if request.method == 'POST':
        username = request.POST['username']
        email = request.POST['email']
        password = request.POST['password']

        # 检查是否已经存在相同用户名或邮箱的用户
        if User.objects.filter(username=username).exists():
            messages.error(request, '用户名已存在！')
            return redirect('register')
        elif User.objects.filter(email=email).exists():
            messages.error(request, '邮箱已被注册！')
            return redirect('register')

        # 创建新用户
        user = User.objects.create_user(username=username, email=email, password=password)
        user.save()

        messages.success(request, '注册成功！')
        return redirect('login')  # 注册成功后跳转到登录页面
    else:
        return render(request, 'register.html')  # 渲染 register.html

# 登录视图
def login_view(request):
    # return render(request, 'login.html')  # 渲染 templates/login.html

    print("开始登录0")
    if request.method == 'POST':
        print("开始登录")
        username = request.POST['username']
        password = request.POST['password']
        print(f"用户名是：{username}")
        # 验证用户名和密码
        user = authenticate(request, username=username, password=password)

        if user is not None:
            login(request, user)  # 登录用户
            print("登录成功")
            messages.success(request, '登录成功！')

            # return redirect('homepage')  # 登录后跳转到首页
            return redirect('titanic:home')

        else:
            messages.error(request, '用户名或密码错误！')
            print("登录失败")
            return redirect('login')
    else:
        print("登录失败2")
        return render(request, 'login.html')  # 渲染 login.html


# 首页视图
def homepage(request):
    username = request.user.username if request.user.is_authenticated else None
    return render(request, 'index.html', {'username': username})


urlpatterns = [
    path('admin/', admin.site.urls),
    path('', homepage, name="homepage"),
    path('titanic/', include('django_titanic.urls')),
    path('gpt/', include('django_gpt.urls')),
    path('register/', register_view, name='register'),  # 注册页面路由
    path('login/', login_view, name='login'),  # 登录页面路由
    path('logout/', auth_views.LogoutView.as_view(), name='logout'),  # 退出路由

]
